<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected $_logger;
	public $frontController;
	
//	protected function _initSessions() {
//	 	$this->bootstrap('session');
//	}
	
	protected function _initNavigation()
	{		
//		//$this->bootstrap('view');
//		$view = $this->getResource('view');
////		echo '<pre>';
////		var_dump($view);
////		echo '</pre>'; exit();
//		if($view) echo 'not null'; else echo 'null'; exit();	
//		
//		$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');		
//		$navigation = new Zend_Navigation($config);
//		$view->navigation($navigation);
	}
	
	protected function _initTimeZone()
	{		
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}
	
	protected function _initAutoloaderNamespace()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->registerNamespace('TBB_');
		$autoloader->registerNamespace('Doctrine_');  

		// Why Zend_ and ZendX_ is in autoloader namespaces? Are they default?
//		var_dump($autoloader->getRegisteredNamespaces());
//		exit();
	}
	
	protected function _initDefaultModuleAutoloader()
	{		
		// Just do this when we don't have bootstrap files for each module
		/*
		$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
										'namespace' => 'Customer',
										'basePath' => APPLICATION_PATH . '/modules/customer'
		));		
		$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
										'namespace' => 'Admin',
										'basePath' => APPLICATION_PATH . '/modules/admin'
		));
		*/
	}
	
	protected function _initRoutes()
	{
		$this->bootstrap('frontController');
		$router = $this->frontController->getRouter();

		////////////// Module controller action /////////////
		$route = new Zend_Controller_Router_Route(
			':module/controller:/:action/*',
			array(
				'module' => 'customer',
				'controller' => 'index',
				'action' => 'index',
				'isAdmin' => false
			)
		);
		$router->addRoute('module_controller_action', $route);		
		
		////////////// Trip list ////////////// 
		$route = new Zend_Controller_Router_Route(
			'trip/list/page/:page',
			array(
				'module' => 'customer',
				'controller' => 'trip',
				'action' => 'list',
			),
			array(
				'page' => '\d+'
			)
		);
		$router->addRoute('trip_list_page', $route);
		
		////////////// Route list /////////////		
		$route = new Zend_Controller_Router_Route(
			'admin/route/list/page/:page',
			array(
				'module' => 'admin',
				'controller' => 'route',
				'action' => 'list',
			),
			array(
				'page' => '\d+'
			)
		);
		$router->addRoute('admin_route_list_page', $route);
		
		////////////// User list /////////////
		$route = new Zend_Controller_Router_Route(
			'admin/users/list/page/:page',
			array(
				'module' => 'admin',
				'controller' => 'users',
				'action' => 'list',
			),
			array(
				'page' => '\d+'
			)
		);
		$router->addRoute('admin_users_list_page', $route);
		
		////////////// Role list /////////////
		$route = new Zend_Controller_Router_Route(
			'admin/role/list/page/:page',
			array(
				'module' => 'admin',
				'controller' => 'role',
				'action' => 'list',
			),
			array(
				'page' => '\d+'
			)
		);
		$router->addRoute('admin_role_list_page', $route);

		////////////// Ticket list /////////////
		$route = new Zend_Controller_Router_Route (
			'ticket/list/trip/:trip_id',
			array(
				'module' => 'customer',
				'controller' => 'ticket',
				'action' => 'list'
			),
			array(
				'trip_id' => '\+d'
			)
		);
		$router->addRoute('ticket_list_tripID', $route);
	}	
	
	protected function _initDb()
	{
		$config = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV );
		$adapter = $config->resources->db->adapter;
		$db = Zend_Db::factory ( $adapter, $config->resources->db->params->toArray () );
		$db->query ( "SET NAMES 'utf8' " );
		Zend_Db_Table::setDefaultAdapter ( $db );
		return $db;
	}
	
	protected function _initProfiler()
	{
//		$db = Zend_Db_Table::getDefaultAdapter();
//		$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
//		$profiler->setEnabled(true);
//
//		// Attach the profiler to your db adapter
//		$db->setProfiler($profiler);
	}
	
	
	
	protected function _initView()
	{
		// Initialize view
		$view = new Zend_View();
		
		// Set encoding and doctype
		$view->setEncoding('utf-8');		
		$view->doctype('HTML4_STRICT');
		
		
		//====================== Set the content type, language and title ====================//
		$view->headMeta()->appendHttpEquiv(
			'Content-Type', 'text/html; charset=utf-8');
		$view->headMeta()->appendHttpEquiv('Content-Language', 'en-US');
		$view->headMeta()->appendName('description', 'tour bus, booking, website');
		$view->headMeta()->appendName('keywords', 'Tour bus booking website is where we can book tickets for trip');
		$view->headTitle('Tour Bus Booking');
		//===================================================================================//
		
		
		//================== Include blueprint css framework =============//
		$view->headLink()->appendStylesheet('/css/blueprint/screen.css', 'screen, projection');
		$view->headLink()->appendStylesheet('/css/blueprint/print.css', 'print');
		$view->headLink()->appendStylesheet('/css/blueprint/ie.css', 'screen, projection', 'lt IE8');
		//===============================================================//

		
		//=============== Include jquery library ================//
		$view->headScript()->appendFile('/scripts/libraries/jquery-1.4.2.min.js', 'text/javascript');
		$view->headScript()->appendFile('/scripts/libraries/jquery.tablesorter.js', 'text/javascript');
		$view->headScript()->appendFile('/scripts/libraries/jquery.dataTables.js', 'text/javascript');
		//=============== add jquery library ================//

		
		// Why can't?????
		// add jquery UI library
		//$view->headScript()->appendFile('/scripts/libraries/jquery-ui-1.8.7.custom.min.js', 'text/javascript');
		//$view->headLink()->appendStylesheet('/css/ui-lightness/jquery-ui-1.8.7.custom.css');
		
		
		//========== Set skin for website ==========//
		$skin = 'blues';
		$view->skin = $skin;
		Zend_Registry::set('skin', $skin);	
		//==========================================//
		
		
		//================== Add helper path ====================//
		// this helper path for LoadSkin
		$view->addHelperPath('TBB/View/Helper', 'TBB_View_Helper');
		
		// this helper path for ZendX datepicker
		$view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');		
		//=======================================================//
			
		
		//================ Include jquery UI (this way by Stackoverflow, not me)==================//
		$view->jQuery()
		->addStylesheet('/css/ui-lightness/jquery-ui-1.8.7.custom.css')
		->setLocalPath('/scripts/libraries/jquery-1.4.2.min.js')
        ->setUiLocalPath('/scripts/libraries/jquery-ui-1.8.7.custom.min.js');
        //====================================================================================//
		
        
		//================ Set view for view renderer ===============//
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);
		//===========================================================//
		
		
		// Return it, so that it can be stored by the bootstrap
		return $view;
	}

}

